package cn.itcast.nems.oldems.converter;

import cn.itcast.nems.common.account.AccountInfo;
import cn.itcast.nems.common.utils.BigDecimalUtil;
import cn.itcast.nems.oldems.dto.StudentJoinClassInfo;
import cn.itcast.nems.oldems.dto.StudentRecordCirculation;
import cn.itcast.nems.oldems.dto.oldapi.ClassInfoDto;
import cn.itcast.nems.oldems.dto.oldapi.ClassPhaseInfoDto;
import cn.itcast.nems.oldems.dto.oldapi.DiscountDto;
import cn.itcast.nems.oldems.dto.oldapi.StudentJoinClassCirculation;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * author liyong
 * create: 2024-03-14 15:01:09
 **/
public class StudentJoinClassCirculationConverter {

    public static StudentJoinClassCirculation convert(Integer orgiginSjcId, StudentRecordCirculation circulation) {
        StudentJoinClassCirculation result = new StudentJoinClassCirculation();
        result.setOriginSjcIdList(Set.of(orgiginSjcId));
        result.setOriginSjcId(orgiginSjcId);

        ClassInfoDto originClass = convertToClassInfo(circulation.getOriginStudentJoinClassInfo());
        result.setOriginClass(originClass);
        ClassPhaseInfoDto originCpi = convertToClassPhaseInfoDto(circulation.getOriginStudentJoinClassInfo());
        result.setOriginClassPhases(List.of(originCpi));

        ClassInfoDto targetClass = convertToClassInfo(circulation.getTargetStudentJoinClassInfo());
        result.setTargetClass(targetClass);
        ClassPhaseInfoDto targetCpi = convertToClassPhaseInfoDto(circulation.getTargetStudentJoinClassInfo());
        result.setTargetClassPhases(List.of(targetCpi));

        final String cw = circulation.getCirculationWay().name();
        result.setCirculationWay(cw);
        final AccountInfo operator = circulation.getOperator();
        result.setOperatorEmail(operator.getEmail());
        result.setOperatorUsername(operator.getName());
        result.setOperatorRealName(operator.getName());
        return result;
    }

    private static ClassInfoDto convertToClassInfo(StudentJoinClassInfo studentJoinClassInfo) {
        ClassInfoDto result = new ClassInfoDto();
        result.setClassId(studentJoinClassInfo.getClazzId());
        result.setOpenClass(studentJoinClassInfo.getOpenClass());
        result.setEntryDate(studentJoinClassInfo.getInDate());
        result.setLeaveDate(studentJoinClassInfo.getOutDate());
        final Integer price = studentJoinClassInfo.getPrice();
        if (Objects.nonNull(price)) {
            final BigDecimal fee = BigDecimalUtil.centIntToBigDecimal(price);
            result.setContractFee(fee);
        }
        String pt = studentJoinClassInfo.getPaymentType().name();
        result.setPaymentType(pt);
        return result;
    }

    private static ClassPhaseInfoDto convertToClassPhaseInfoDto(StudentJoinClassInfo studentJoinClassInfo) {
        ClassPhaseInfoDto result = new ClassPhaseInfoDto();
        result.setTotalDays(studentJoinClassInfo.getServiceDays());
        result.setAlreadyLearnDays(studentJoinClassInfo.getActualServiceDays());
        result.setMultiChannel(studentJoinClassInfo.getMultiChannel());
        result.setOriJournalChannelId(studentJoinClassInfo.getOriJournalChannelId());
        result.setJournalChannelId(studentJoinClassInfo.getJournalChannelId());
        result.setJournalChannelName(studentJoinClassInfo.getJournalChannelName());
        final List<Integer> discountIds = studentJoinClassInfo.getDiscountIds();
        if (discountIds != null && !discountIds.isEmpty()) {
            List<DiscountDto> discountList = discountIds.stream().map(id -> {
                DiscountDto dto = new DiscountDto();
                dto.setDiscountId(id);
                return dto;
            }).toList();
            result.setDiscountList(discountList);
        }
        return result;
    }

}
